<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/7
 * Time: 1:51
 */

namespace app\api\controller\v2;

use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;

/**
 * @title 消息中心
 * @desc
 * @url
 * @version 1.3.0
 * Class Message
 * @package app\api\controller\v2
 */
class Message extends Auth
{
    protected $restActionList = ['index', 'read', 'save', 'update', 'delete'];

    /**
     * 消息列表
     * @return \think\response\Json
     * @throws \Exception
     */
    public function index()
    {
        $type = input('get.type/d');
        $page = input('get.page/d', 1);

        $MsgModel = new MessageCenter();

        $uid = $this->user['id'];
        $map['range'] = array(['=', 0], ['=', $this->user['id']], 'or');
        $map['status'] = array('neq', -1);
        $map['type'] = $type;

        // 获取当前用户的所有消息
        $data = $MsgModel->get_user_msg_id(['uid' => $uid, 'type' => $type, 'page' => $page]);
        $tmp = [];
        try
        {
            if ($data['data'])
            {
                foreach ($data['data'] as $k => $v)
                {
                    //若消息中心数据未删除,进行数据显示
                    if ($v['cid'] == 0)
                    {
                        $icon_url = \config('message.default');
                    } elseif ($v['cid'] == 1)
                    {
                        $icon_url = \config('message.rectification');
                    } elseif ($v['cid'] == 2)
                    {
                        $icon_url = \config('message.lucky_star');
                    } else
                    {
                        $icon_url = \config('message.default');
                    }
                    //反转义
                    $tmp[] = [
                        'id' => $v['id'],
                        'title' => $v['title'],
                        'content' => $v['content'],
                        'link' => htmlspecialchars_decode($v['link']),
                        'target' => $v['target'],
                        'create_time' => $v['create_time'],
                        'icon_url' => $icon_url,
                        'is_read' => $v['is_read']
                    ];
                }

            }
            $data['data'] = $tmp;
        }
        catch (\Exception $e)
        {
            \think\Log::write('获取消息中心列表失败:' . $e->getMessage() . '--------用户id:' . $uid, 'log', true);
            $data = [];
        }

        //将数据根据创建时间排序
        if ($data['data'])
        {
            array_multisort(array_column($data['data'], 'id'), SORT_NUMERIC, SORT_DESC, $data['data']);
        }
        return show(\config('code.success'), '成功', $data);
    }

    /**
     * 获取不同类型的消息未读数量
     * @return \think\response\Json
     * @throws \Exception
     */
    public function is_read()
    {
        $MsgModel = new MessageCenter();

        $uid = $this->user['id'];

        $arr = [];

        //获取用户全部未读系统消息id
        $system_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 1]);
        //获取用户全部未读服务消息id
        $server_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 2]);

        $arr['type1'] = count($system_msg_id);
        $arr['type2'] = count($server_msg_id);

        // 获取未阅读的回复量
        $replyModel = new ArticleCommentReply();
        $unread_ids = $replyModel->getUnReadReply(['ucid' => $this->user['uc_id'], 'uid' => $this->user['id']]);
        if ($unread_ids)
        {
            $arr['type3'] = count($unread_ids);
        } else
        {
            $arr['type3'] = 0;
        }
        return show(200, '成功', $arr);
    }

    /**
     * 将未读状态改为已读
     * @return \think\response\Json
     * @throws \Exception
     */
    public function change_read()
    {
        $id = input('id/d');
        $MsgModel = new MessageCenter();

        $data = $MsgModel->batchGetInfoByIds([$id]);
        if ($data)
        {
            $data = current($data);
            $res = $MsgModel->UpdateData($this->user['id'], $data['type'], [$id], ['is_read' => 1]);
            if ($res)
            {
                return show(config('code.success'), '操作成功');
            }
        }
        return show(config('code.error'), '操作失败');
    }
}
